package com.dzjcloud.dzj.gateway.config;

import com.dzjcloud.dzj.common.redis.constant.RedisInfoConstant;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author: 董泽建
 * @create: 2024-03-26 16:42
 * @Description: redis监听订阅
 */
@Configuration
public class RedisSubConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory factory, RedisMessageListener listener) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(factory);
        //订阅频道  这个container 可以添加多个 messageListener  channel_demo频道名称
        container.addMessageListener(listener, new ChannelTopic(RedisInfoConstant.ROUTER_RELOAD_REDIS_SUBSCRIBE_KEY));
        //container.addMessageListener(listener, new ChannelTopic("redis.news"));
        return container;
    }

    @Bean
    public StringRedisSerializer stringRedisSerializer() {
        return new StringRedisSerializer();
    }
}
